package 多线程和队列.辅助类JUC;

import java.util.concurrent.Semaphore;

/**
 * @author 孔令富  2021/12/20
 */
public class SemphoreDemo {

    public static void main(String[] args) {
        // 有三个停车位 
        Semaphore  semaphore  = new Semaphore(3);

        //  模拟汽车
        for (int i = 1; i <=6; i++) {
            new Thread(()->{
                try {
                    System.out.println(Thread.currentThread().getName()+",抢到了车位");
                    semaphore.acquire();
                    System.out.println(Thread.currentThread().getName()+",离开车位");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    semaphore.release();
                }


            },String.valueOf(i)).start();
        }


    }
}
